<?php
/**
 * @author qingyu8@staff.weibo.com
 * @date 2018/03/11
 */
namespace common\logic;

use common\logic\Tour;
use common\models\Boat as BoatModel;
use common\logic\Trip;
use common\models\BookerItem;
use common\models\Trip as TripModel;
use common\logic\TripProductRel;
use yii\helpers\Inflector;

class Boat {
    public static function getBasicInfo($boatId) {
        $boat = BoatModel::getOne("id=$boatId");
        return [
            'name_cn' => $boat['name_cn'],
            'name_en' => $boat['name_en'],
            'main_image' => $boat['main_image'],
        ];
    }

    public static function getInfo($boatId) {
        if (empty($boatId)) {
            return [
                'err' => 1,
                '参数错误'
            ];
        }
        $item = BoatModel::getOne(['id' => $boatId]);

        //$facilities=BookerItem::getList("type_en='Onboard Facilities'");
        $savedFeatures=  json_decode($item['feature_json'],true);
        if (!isset($savedFeatures['Onboard Facilities'])) {
            $savedFeatures['Onboard Facilities'] = [];
        }
        if (!isset($savedFeatures['Food & drinks'])) {
            $savedFeatures['Food & drinks'] = [];
        }

        $names = '\'' . implode('\',\'', $savedFeatures['Onboard Facilities']) . '\'';
        $features['devices'] = BookerItem::getColumn("type_en='Onboard Facilities' AND name_en IN ($names)", 'name_cn');

        $names = '\'' . implode('\',\'', $savedFeatures['Food & drinks']) . '\'';
        $features['food'] = BookerItem::getColumn("type_en='Food & drinks' AND name_en IN ($names)", 'name_cn');
        $features['technical'] = [
            'yearBuilt' => $item['year_built'],
            'width' => $item['width'],
            'length' => $item['length'],
            'material' => $item['material'],
        ];

        $boatInfo = [
            'id' => $item['id'],
            'nameCN' => $item['name_cn'],
            'nameEN' => $item['name_en'],
            'cabinPic' => $item['cabin_pic'],
            'locationName' => $item['countries_cn'],
            'yearBuilt' => $item['year_built'],
            'width' => $item['width'],
            'length' => $item['length'],
            'material' => $item['material_cn'],
            'nitrox' => $item['nitrox'], //No,Free,Surcharge
            'wifi' => $item['wifi'],
            'diving_equipment' => $item['diving_equipment'],
            'tech_diving_friendly' => $item['tech_diving_friendly'],
            'languages' => $item['languages_cn'],
            'roomNum' => $item['cabin_total'],
            'descr' => $item['desc_cn'],
            'gallery' => json_decode($item['gallery_json'], true) ?: [],
            'policy' => json_decode($item['policy_cn'], true) ?: [],
        ];
        $prices = Occupancy::getBoatsPrice([$boatInfo['id']]);
        $boatInfo['price'] = [
            'value' => $prices ? $prices[$boatInfo['id']] : 0,
            'unit' => '￥'
        ];
        $tour = Tour::getTourInfoByBoat($boatInfo['id']);
        return [
            'err' => 0,
            'data' => [
                'boatInfo' => $boatInfo,
                'features' => $features,
                'includes' => $tour['included_cn'] ?: '',
                'excludes' => $tour['notincluded_cn'] ?: '空',
                'policy' => $boatInfo['policy'],
            ]
        ];
    }

    public static function getHot() {
        $r = BoatModel::getList('', '*', '', 0, 3);
        $list = [];
        foreach ($r as $v) {
            $list[] = [
                "imageUrl" => json_decode($v['gallery_json'], true)[0] ?? '',
                "name" => explode(',', $v['countries_cn'])[0],
                "eName" => explode(',', $v['countries'])[0],
                "url" => ""
            ];
        }
        return $list;
    }

    public static function getRecommendBoats($location, $cursor = 0, $limit = 4) {
        $locations = [
            [
                'nameCN' => '全部',
                'nameEN' => '',
                'id' => '',
                'boatIds' => [1,2,3,4,5,6,7,8]
            ],
            [
                'nameCN' => '印度尼西亚',
                'nameEN' => 'Indonesia',
                'id' => '',
                'boatIds' => [1,7,16,29,102,104,107,112]
            ],
            [
                'nameCN' => '埃及',
                'nameEN' => 'Egypt',
                'id' => '',
                'boatIds' => [55,59,65,68,14,17,18,21]
            ],
            [
                'nameCN' => '马尔代夫',
                'nameEN' => 'Maldives',
                'id' => '',
                'boatIds' => [27,28,32,33,49,50,72,74]
            ],
            [
                'nameCN' => '墨西哥',
                'nameEN' => 'Mexico',
                'id' => '',
                'boatIds' => [109,15,24,31,61,111,119,138]
            ],
            [
                'nameCN' => '泰国',
                'nameEN' => 'Mexico',
                'id' => '',
                'boatIds' => [57,93,94,125,176,180,189,188]
            ],
        ];
        foreach ($locations as $k => $location) {
            $ids = implode(',', $location['boatIds']);
            $r = BoatModel::getList("id IN ($ids)", '*', '', $cursor, $limit);
            $data = [];
            $boatIds = [];
            foreach ($r as $v) {
                $data[] = [
                    'id' => $v['id'],
                    "imageUrl" => $v['main_image'],
                    "boatNameCN" => $v['name_cn'],
                    "boatNameEN" => $v['name_en'],
                    'locationName' => $v['countries_cn'],
                    'wifi'=>$v['wifi'],
                    'nitrox' => $v['nitrox'],
                    'languages' => $v['languages_cn']
                ];
                $boatIds[] = $v['id'];
            }
            $priceMap = Occupancy::getBoatsPrice($boatIds);
            $minPrice = min($priceMap);
            foreach ($data as $key => $v) {
                $data[$key]['price'] = [
                    'value' => isset($priceMap[$v['id']]) ? (int)$priceMap[$v['id']] : $minPrice,
                    'unit' => '￥'
                ];
            }
            $locations[$k]['list'] = $data;
        }
        return $locations;
    }

    /**
     * @param string $kw 关键词
     * @param string $route 国家
     * @param string $plan 天数
     * @param string $level 等级
     * @param string $device 设备
     * @param string $special 特色
     * @param string $date 旅行时间
     * @param int $limit
     * @param int $page
     * @param string $orderByKey price|价格 rate|评价 built|建造年代
     * @param int $orderBy 1|正序 -1|倒序
     */
    public static function search($kw, $route, $plan, $level, $device, $special, $date, $limit, $page, $orderByKey, $orderBy) {

    }
}
